const jwt = require('jsonwebtoken');
const config = require('../config/config.json');
const util = require('util');
const verify = util.promisify(jwt.verify);

module.exports = async (ctx, next) => {
  const token = ctx.header.authorization;
  if (token) {
    let payload = await verify(token.split(' ')[1], config.sign);
    ctx.user = {
      user_name: payload.user_name,
      id: payload.id
    };
  }
  await next();
}